<template>
  <div class="content">
    <hb-table
      :listData="dataList"
      :listCount="dataCount"
      v-bind="pageContentConfig"
      v-model:page="pageInfo"
    >
      <template #headerHandler>
        <el-button
          type="primary"
          v-if="isCreate"
          @click="handleCreateDataClick"
        >
          创建用户
        </el-button>
      </template>
      <template #createAt="scope">
        {{ $filters.formatTime(scope.row.createAt) }}
      </template>
      <template #updateAt="scope">
        {{ $filters.formatTime(scope.row.updateAt) }}
      </template>
      <template #operation="scope">
        <el-button
          type="text"
          icon="editPen"
          size="small"
          v-if="isUpdate"
          @click="handleEditDataClick(scope.row)"
        >
          编辑
        </el-button>
        <el-button
          type="text"
          icon="Delete"
          size="small"
          v-if="isDelete"
          @click="handleDeleteDataClick(scope.row)"
        >
          删除
        </el-button>
      </template>

      <template
        v-for="item in scopeList"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </hb-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import HbTable from '@/base-ui/table'
import { useStore } from '@/store'
import { usePermission } from '@/hooks/use-permission'
import { ElMessageBox, ElMessage } from 'element-plus'

export default defineComponent({
  components: {
    HbTable
  },
  props: {
    pageContentConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['handleCreateData', 'handleEditData'],
  setup(props, { emit }) {
    const store = useStore()

    const isCreate = usePermission(props.pageName, 'create')
    const isDelete = usePermission(props.pageName, 'delete')
    const isUpdate = usePermission(props.pageName, 'update')
    const isQuery = usePermission(props.pageName, 'query')

    //监听 pageInfo 改变

    const pageInfo = ref({ currentPage: 1, pageSize: 10 })

    watch(pageInfo, () => getPageListData())

    // 发送请求

    const getPageListData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }

    getPageListData()

    // 获取数据 List和Count

    const dataList = computed(() =>
      store.getters[`system/pageListData`](props.pageName)
    )

    const dataCount = computed(() =>
      store.getters[`system/pageListCount`](props.pageName)
    )

    // 获取scopeList
    const scopeList = props.pageContentConfig?.propList.filter((item: any) => {
      if (item.slotName === 'createAt') return false
      if (item.slotName === 'updateAt') return false
      if (item.slotName === 'operation') return false
      return true
    })

    //按钮 事件
    const handleCreateDataClick = () => {
      emit('handleCreateData')
    }

    const handleEditDataClick = (item: any) => {
      emit('handleEditData', item)
    }

    const handleDeleteDataClick = (item: any) => {
      ElMessageBox.confirm('确认是否删除?', '删除', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          store.dispatch('system/deletePageDataAction', {
            pageName: props.pageName,
            id: item.id
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消删除'
          })
        })
    }

    return {
      dataList,
      dataCount,
      getPageListData,
      pageInfo,
      scopeList,
      isCreate,
      isDelete,
      isUpdate,
      handleCreateDataClick,
      handleEditDataClick,
      handleDeleteDataClick
    }
  }
})
</script>

<style scoped>
.content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
